package com.kalvan.web.exception;

import com.kalvan.client.constant.CommonReturnCode;
import com.kalvan.client.exception.BizException;
import com.kalvan.client.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
 * extends ResponseEntityExceptionHandler
 *
 * @author chenliang
 */
@Slf4j
public class ExceptionHandle {

    public static final String EXCEPTION = "Exception";
    public static final String ERROR = "### Error";
    public static final String EXCEPTION1 = "exception";

    public ExceptionHandle() {
        log.info("init ExceptionHandle...");
    }

    /**
     * 处理Get请求中 使用@Valid 验证路径中请求实体校验失败后抛出的异常，详情继续往下看代码
     *
     * @param e 异常
     * @return Response
     */
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Response bindExceptionHandler(BindException e) {
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
        return Response.buildResult(CommonReturnCode.PARAM_ILLEGAL.formatDesc(message));
    }

    /**
     * 处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationException
     *
     * @param e 异常
     * @return Response
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public Response constraintViolationExceptionHandler(ConstraintViolationException e) {
        String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
        return Response.buildResult(CommonReturnCode.PARAM_ILLEGAL.formatDesc(message));
    }

    /**
     * 处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常。
     *
     * @param e 异常
     * @return Response
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Response methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
        return Response.buildResult(CommonReturnCode.PARAM_ILLEGAL.formatDesc(message));
    }

    /**
     * BizException
     *
     * @param e 异常
     * @return Response
     */
    @ExceptionHandler(BizException.class)
    @ResponseBody
    public Response bizExceptionHandler(BizException e) {
        return Response.buildResult(e);
    }

    /**
     * 其它未处理异常
     *
     * @param e 异常
     * @return Response
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Response errorHandler(Exception e) {
        log.error("异常", e);
        // 异常信息显示转换
        String message = e.getMessage();
        if (message == null || message.contains(EXCEPTION) || message.contains(EXCEPTION1) || message.contains(ERROR)) {
            message = "";
        }
        //未知异常
        return Response.buildResult(CommonReturnCode.ERROR.formatDesc(message));
    }
}